package com.pt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pt.common.ApiResponse;
import com.pt.dto.request.CommentRequest;
import com.pt.entity.Comment;
import com.pt.entity.User;
import com.pt.service.CommentService;
import com.pt.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/comments")
@RequiredArgsConstructor
@Api(tags = "评论管理")
public class CommentController {
    
    private final CommentService commentService;
    private final UserService userService;
    
    @GetMapping("/torrent/{torrentId}")
    @ApiOperation("获取种子评论列表")
    public ApiResponse<Page<Comment>> getTorrentComments(
            @ApiParam("种子ID") @PathVariable Long torrentId,
            @ApiParam("页码") @RequestParam(defaultValue = "1") int page,
            @ApiParam("每页数量") @RequestParam(defaultValue = "20") int size) {
        
        Page<Comment> comments = commentService.getTorrentComments(torrentId, page, size);
        return ApiResponse.success(comments);
    }
    
    @PostMapping
    @ApiOperation("发表评论")
    @PreAuthorize("isAuthenticated()")
    public ApiResponse<Comment> addComment(@Valid @RequestBody CommentRequest request) {
        User currentUser = userService.getCurrentUser();
        Comment comment = commentService.addComment(request, currentUser.getId());
        return ApiResponse.success(comment);
    }
    
    @PostMapping("/{commentId}/like")
    @ApiOperation("点赞评论")
    @PreAuthorize("isAuthenticated()")
    public ApiResponse<String> likeComment(@ApiParam("评论ID") @PathVariable Long commentId) {
        User currentUser = userService.getCurrentUser();
        commentService.likeComment(commentId, currentUser.getId());
        return ApiResponse.success("点赞成功");
    }
    
    @DeleteMapping("/{commentId}/like")
    @ApiOperation("取消点赞")
    @PreAuthorize("isAuthenticated()")
    public ApiResponse<String> unlikeComment(@ApiParam("评论ID") @PathVariable Long commentId) {
        User currentUser = userService.getCurrentUser();
        commentService.unlikeComment(commentId, currentUser.getId());
        return ApiResponse.success("取消点赞成功");
    }
    
    @PostMapping("/{commentId}/report")
    @ApiOperation("举报评论")
    @PreAuthorize("isAuthenticated()")
    public ApiResponse<String> reportComment(
            @ApiParam("评论ID") @PathVariable Long commentId,
            @ApiParam("举报原因") @RequestParam String reason) {
        User currentUser = userService.getCurrentUser();
        commentService.reportComment(commentId, currentUser.getId(), reason);
        return ApiResponse.success("举报成功");
    }

    @DeleteMapping("/{commentId}")
    @ApiOperation("删除评论")
    @PreAuthorize("isAuthenticated()")
    public ApiResponse<String> deleteComment(@ApiParam("评论ID") @PathVariable Long commentId) {
        User currentUser = userService.getCurrentUser();
        commentService.deleteComment(commentId, currentUser.getId());
        return ApiResponse.success("删除成功");
    }
} 